package com.langcollab.languagementor.controller
{
    import com.brightworks.util.Debug;
    import com.langcollab.languagementor.event.Event_Base;
    import com.langcollab.languagementor.model.Callbacks;

    import flash.events.Event;
    import flash.events.IEventDispatcher;

    public class Command_Base
    {
        [Dispatcher]
        public var dispatcher:IEventDispatcher;

        private function get callbacks():Callbacks
        {
            var result:Callbacks;
            if (_event.callbacks)
                result = _event.callbacks;
            return result;
        }

        private var _event:Event_Base;

        public function get event():Event
        {
            return _event;
        }

        public function set event(value:Event):void
        {
            _event = Event_Base(value);
        }

        public function fault(info:Object = null):void
        {
            if (callbacks && (callbacks.fault is Function))
            {
                callbacks.fault(info);
            }
            else
            {
                Debug.error_Fatal(["Command_Base.fault()", info]);
            }
        }

        public function result(data:Object = null):void
        {
            if (callbacks && (callbacks.result is Function))
            {
                callbacks.result(data);
            }
            else
            {
                Debug.error_Fatal(["Command_Base.result(): result function instance isn't available", data]);
            }
        }

        protected function dispatchEvent(event:Event_Base):void
        {
            dispatcher.dispatchEvent(event);
        }

    }
}

